package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.FragmentPowerBinding;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.PowerPresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class PowerFragment extends BaseFragment {

    public static final String TAG = "PowerFragment";

    private FragmentPowerBinding mBinding;


    public static PowerFragment newInstance() {
        final PowerFragment fragment = new PowerFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_power, container, false);
        mBinding.setPowerPresenter(powerPresenter);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private final PowerPresenter powerPresenter = new PowerPresenter() {
        @Override
        public void onPowerOffBtClick() {
            final String method = "powerOff";
            if (NetService.getInstance().isDisable(method)){
                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
                return;
            }
            NetService.getInstance()
                    .powerOff(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid())
                    .timeout(10, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>(getActivity(), true) {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.d("onSuccess : " + responseEntity.toString());
                            TSnackbarUtil.TSBSuccess(getActivity(), "已发送");
                        }

                        @Override
                        public String onOver() {
                            return method;
                        }
                    });
        }

        @Override
        public void onRebootClick() {
            final String method = "reboot";
            if (NetService.getInstance().isDisable(method)){
                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
                return;
            }
            NetService.getInstance()
                    .reboot(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid())
                    .timeout(10, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>(getActivity(), true) {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.d("onSuccess : " + responseEntity.toString());
                            TSnackbarUtil.TSBSuccess(getActivity(), "已发送");
                        }

                        @Override
                        public String onOver() {
                            return method;
                        }
                    });
        }
    };

}
